# bkpkg.py
# 
# since related modules developed under component/pikapython are not under git
# control,
# it need to backup to packages and commit to git repo
# this program provide the backup of all those files
#

import os
import shutil

PKG_PATH="packages"

jobs={'m5epd':
      [
          ['components/pikapython/pikascript-lib/m5epd'],          
          ["components/pikapython","m5epd.py","_m5epd.pyi","m5epd_test.py"],
      ],
      'epd9in7':
      [
          ['components/pikapython/pikascript-lib/epd9in7'],          
          ["components/pikapython","epd9in7.py","_epd9in7.pyi","epd9in7_test.py"],
      ],      
      'm5stack':
      [
          ['components/pikapython/pikascript-lib/m5stack'],          
          ["components/pikapython","m5stack.py","_m5stack.pyi","m5stack_initdata.py","lcd_util.py"],
      ],
      
      'misc_util':
      [
          ["components/pikapython/pikascript-lib/misc_util"],          
          ["components/pikapython","misc_util.py","_misc_util.pyi"],
      ],
      'time_sync':
      [
          ["components/pikapython/pikascript-lib/time_sync"],          
          ["components/pikapython","time_sync.py","_time_sync.pyi"],
      ],            
      'framebuf':
      [
          ["components/pikapython/pikascript-lib/framebuf"],          
          ["components/pikapython","framebuf.py","_framebuf.pyi"],
      ],
      'aht20':     
      [
          ["components/pikapython/pikascript-lib/aht20"],          
          ["components/pikapython","aht20.py","_aht20.pyi","aht20_unittest.py"],
      ],
      'flashdb':     
      [
          ["components/pikapython/pikascript-lib/flashdb"],          
          ["components/pikapython","flashdb.py","_flashdb.pyi","flashdb_utest.py"],
      ],     
}

      

def main():
    keys = list(jobs.keys())
    keys.sort()
    for key in keys:
        pkg_path=os.path.join(PKG_PATH, key)
        jlist = jobs[key]
        for job in jlist:
            print('\njob: %s' % job)
            if len(job)>1:
                # copy specific files
                src_path = job[0]
                for fn in job[1:]:
                    src_fn = os.path.join(job[0],fn)
                    dest_fn = os.path.join(pkg_path, fn)
                    try:
                        shutil.copyfile(src_fn, dest_fn)
                        print('copy %s -> %s ok' % (src_fn, dest_fn))
                    except:
                        print('[error] copy %s -> %s fail' % (src_fn, dest_fn))
            else:
                # copy all file in src
                src_path = job[0]
                if not os.path.isdir(src_path):
                    print('[error] %s not exists' % src_path)
                    break
                
                src_fns = os.listdir(src_path)
                print('src_fns:%s' % src_fns)
                for fn in src_fns:
                    if fn.endswith('~') or fn.endswith('#'):
                        continue
                    src_fn = os.path.join(job[0],fn)
                    dest_fn = os.path.join(pkg_path, fn)
                    try:
                        shutil.copyfile(src_fn, dest_fn)
                        print("copy %s -> %s ok" % (src_fn, dest_fn))
                    except:
                        print("[error] copy %s -> %s fail" % (src_fn, dest_fn))
                    
    
if __name__=='__main__':
    main()
